package cn.com.sueframe.serverlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

/**
 * //�������ʱ���г�ʼ������
 * @author anler
 * Create Jul 7, 2012
 */
public class InitServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5485118988339413080L;
	private Map<String,Object> cnMap = new HashMap<String, Object>();
	private Map<String,Object> enMap = new HashMap<String, Object>();
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		String fileDir = config.getServletContext().getRealPath("/");
		loadNationalProperty("CN",fileDir);
		loadNationalProperty("EN",fileDir);
		config.getServletContext().setAttribute("cnMap", cnMap);
		config.getServletContext().setAttribute("enMap", enMap);
	}
	
	private void loadNationalProperty(String country,String fileDir){
		Properties properties = new Properties();
	    String filePath = "";
	    if("CN".equals(country)){
	    	filePath = fileDir + "/WEB-INF/classes/struts_zh_CN.properties";
		}else{
			filePath = fileDir + "/WEB-INF/classes/struts_en_US.properties";
		}
		File srcFile = new File(filePath);
		FileInputStream in;
		try {
			in = new FileInputStream(srcFile);
			properties.load(in);
			Set<Object> set = properties.keySet();
			Iterator<Object> iterator = set.iterator();
			while(iterator.hasNext()){
				String key = String.valueOf(iterator.next());
				if("CN".equals(country)){
					cnMap.put(key, properties.get(key));
				}else{
					enMap.put(key, properties.get(key));
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
